package com.ruoyi.common.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.enums.FileTypeEnum;
import com.ruoyi.common.service.IUploadFileService;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

public class DefaultUploadFileService implements IUploadFileService {

    @Autowired
    private ServerConfig serverConfig;

    @Override
    public String uploadFile(MultipartFile file, String fileType) {

        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getFilePath(FileTypeEnum.getPathUrl(fileType));
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;

//            ajax.put("url", url);
//            ajax.put("fileName", fileName);
//            ajax.put("newFileName", FileUtils.getName(fileName));
//            ajax.put("originalFilename", file.getOriginalFilename());
            return url;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return null;
    }

    @Override
    public void getObjectTag() {

    }
}
